<template>
  <div>
    <el-table
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
      border
      :data="PermissionList"
    >
      <el-table-column label="名称" prop="name"></el-table-column>
      <el-table-column
        label="权限值"
        align="center"
        prop="code"
      ></el-table-column>
      <el-table-column
        label="修改时间"
        align="center"
        prop="updateTime"
      ></el-table-column>
      <el-table-column label="操作" width="300px" align="center">
        <template #="{ row, $index }">
          <el-button
            type="primary"
            size="small"
            :disabled="row.level === 4 ? true : false"
            @click="addPermission(row)"
            >{{ row.level === 3 ? "添加功能" : "添加菜单" }}</el-button
          >
          <el-button
            type="primary"
            size="small"
            :disabled="row.level == 1 ? true : false"
            @click="updatePermission(row)"
            >编辑</el-button
          >
          <el-popconfirm
            :title="`你确定删除${row.name}吗?`"
            @confirm="deleteMenu(row.id)"
          >
            <template #reference>
              <el-button type="danger" size="small" icon="Delete"
                >删除
              </el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!-- 对话框组件:添加或更新 -->
    <el-dialog
      v-model="dialogVisible"
      :title="menuData.id ? '修改菜单' : '添加菜单'"
    >
      <!-- 表单组件:收集新增或编辑的数据 -->
      <el-form>
        <el-form-item label="名称">
          <el-input
            placeholder="请输入菜单名称"
            v-model="menuData.name"
          ></el-input>
        </el-form-item>
        <el-form-item label="权限">
          <el-input
            placeholder="请输入权限值"
            v-model="menuData.code"
          ></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="save"> 确定 </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from "vue";
import { reqAllMenu, reqAddOrUpdateMenu, reqDeleteMenu } from "@/api/acl/menu";
import { ElMessage } from "element-plus";

// 存储菜单数据
const PermissionList = ref([]);
// 控制对话框的显示与隐藏
const dialogVisible = ref(false);
// 添加或更新携带的参数数据
const menuData = ref<any>({});
// 组件挂载完毕渲染数据
onMounted(() => {
  getAllMenu();
});

// 封装渲染方法
const getAllMenu = async () => {
  const res = await reqAllMenu();
  if (res.code === 200) {
    PermissionList.value = res.data;
  }
};

// 添加菜单按钮回调
const addPermission = (row: any) => {
  // 清空表单数据
  menuData.value = {};
  dialogVisible.value = true;
  // 收集新增菜单的level值
  menuData.value.level = row.level + 1;
  // 给谁添加子菜单
  menuData.value.pid = row.id;
};

// 编辑菜单按钮回调
const updatePermission = (row: any) => {
  dialogVisible.value = true;
  // 收集数据
  Object.assign(menuData.value, row);
};

// 确定按钮的回调
const save = async () => {
  // 输入不能为空
  if (!menuData.value.name || !menuData.value.code) {
    ElMessage.warning("请输入菜单名称和权限值");
    return;
  }
  const res = await reqAddOrUpdateMenu(menuData.value);
  if (res.code === 200) {
    // 关闭对话框
    dialogVisible.value = false;
    // 提示信息
    ElMessage.success(menuData.value.id ? "修改成功" : "添加成功");
    // 重新渲染页面
    getAllMenu();
  } else {
    // 添加失败
    ElMessage.error(menuData.value.id ? "修改失败" : "添加失败");
  }
};

// 删除按钮回调
const deleteMenu = async (id: number) => {
  const res = await reqDeleteMenu(id);
  if (res.code === 200) {
    // 提示信息
    ElMessage.success("删除成功");
    // 重新渲染页面
    getAllMenu();
  } else {
    // 添加失败
    ElMessage.error("删除失败");
  }
};
</script>

<style scoped></style>
